package com.supplychain.scm.core.rocketmq.config;

import com.supplychain.scm.core.rocketmq.constant.RocketMqConstant;
import com.supplychain.scm.core.rocketmq.producer.RocketMqProducer;
import com.supplychain.scm.core.rocketmq.storage.IMqMessageStorage;
import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.concurrent.Executor;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName RocketMqAutoConfiguration
 * @createTime 09:03:00 2022/12/06
 */
@Configuration
@ConditionalOnClass({RocketMQTemplate.class})
@AutoConfigureOrder(1000)
@RefreshScope
@EnableConfigurationProperties(RocketMQProperties.class)
@ConditionalOnProperty(prefix = "rocketmq.producer", value = "enabled", havingValue = "true",matchIfMissing = true)
public class RocketMqAutoConfiguration {

    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @Autowired(required = false)
    private IMqMessageStorage mqMessageStorage;
    @Autowired(required = false)
    @Qualifier(value = RocketMqConstant.EXECUTOR_NAME)
    private Executor executor;
    @Value("${spring.application.name}")
    private String name;
    @Value("${rocketmq.producer.store.enabled:true}")
    private Boolean ifStore;

    @Bean
    @ConditionalOnMissingBean
    public RocketMqProducer rocketMqProducer(){
        return new RocketMqProducer(rocketMQTemplate,mqMessageStorage,executor,name,ifStore);
    }
}
